package LeetCode;

import java.util.*;

//滑动窗口最大值
public class No239 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String[] chs = s.split(",");

        int[] a = new int[chs.length];
        for (int i = 0; i < chs.length; i++) {
            a[i] = Integer.parseInt(chs[i]);
        }
        int n = sc.nextInt();
        int[] b = maxSlidingWindow(a,n);

        for (int i = 0; i < b.length; i++) {
            System.out.print("b["+i+"] = " + b[i]);
        }
    }

    public static int[] maxSlidingWindow(int[] nums, int k) {
        int[] a = new int[nums.length-k+1];
        Deque<Integer> deque = new ArrayDeque<>();
        for (int i = 0; i < nums.length; i++) {
            while(!deque.isEmpty()&&nums[deque.getLast()]<nums[i]){
                deque.removeLast();
            }
            deque.add(i);

            int left = i-k+1;
            if(deque.getFirst()<left){
                deque.removeFirst();
            }
            if(left>=0){
                a[left] = nums[deque.getFirst()];
            }
        }
        return a;
    }
}
